home *** CD-ROM | disk | FTP | other *** search
- //------------------------------------------------------------------------
- //
- // structure.prg -- class Structure
- //
- // A custom class for working with structures passed to
- // external functions such as the Windows API. This
- // class is used by the Win32API sample form.
- //
- // The following example shows how to create a two member
- // structure and use the setMember() and getMember() methods
- // to set and get the value of a structure member.
- /*
-
- se = new structExample()
- se.setMember("memberOne", 128)
- ? se.getMember("memberOne")
-
- class structExample of Structure
- super::addMember( TYPE_WORD, "memberOne" )
- super::addMember( TYPE_STRING, "memberTwo", 5)
- endclass
-
- */
- //
- // Visual dBASE Samples Group
- //
- // $Revision: 1.1 $
- //
- // Copyright (c) 1997, Borland International, Inc.
- // All rights reserved.
- //
- //------------------------------------------------------------------------
-
- #include "structapi.h"
-
-
-
-
- class Structure
- local sFolder
- sFolder = SUBSTR( PROGRAM(0), 1, ;
- LEN( PROGRAM( 0 ) ) - LEN("structure.prg") )
- DO (sFolder + "\STRUCTAPI.PRG" )
- this.member = new Array()
- this.value = ""
-
- function addMember( nType, sName, nSize )
- local i
- nSize = IIF(PCOUNT()==3,nSize,0)
- i = this.member.add( new StructMember( sName, nType, nSize ) )
- if ( i > 1)
- this.member[ i ].offset := this.member[ i - 1 ].offset + ;
- this.member[ i - 1 ].size
- endif
- this.value := this.value + ;
- REPLICATE(" ", this.member[i].size)
- class::setMember( sName )
- return ( i )
-
- function getMember( sName )
- local xValue, nBytes, i
- xValue = null
- nBytes = 0
- i = class::findIndex( sName )
-
- if ( i > 0 )
- nType = this.member[i].type
- nOffset = this.member[i].offset
- nSize = this.member[i].size
- do case
- case ( nType == TYPE_WORD OR ;
- nType == TYPE_DWORD OR ;
- nType == TYPE_LONG )
- xValue := GetStructNumber( this.value, ;
- nOffset, ;
- nType )
- case ( nType == TYPE_STRING )
- xValue = REPLICATE(" ", nSize)
- nSize = GetStructStringLen( this.value, nOffset )
- nBytes = GetStructString( this.value, ;
- nOffset, ;
- nSize, ;
- xValue, ;
- nSize)
- endcase
- endif
- return ( xValue )
-
-
- function setMember( sName, xValue )
- local i, nBytes, nType, nOffset, nLength
- i = class::findIndex( sName )
- nBytes = 0
-
- if ( i > 0 )
- nType = this.member[i].type
- nOffset = this.member[i].offset
- nSize = this.member[i].size
- nLength = this.length()
- do case
- case ( nType == TYPE_WORD OR ;
- nType == TYPE_DWORD OR ;
- nType == TYPE_LONG )
- xValue = IIF( PCOUNT() == 2, xValue, 0)
- nBytes := SetStructNumber( this.value, ;
- nOffset, ;
- nType, ;
- nLength, ;
- xValue )
- case ( nType == TYPE_STRING )
- if ( PCOUNT() == 2 )
- xValue = SUBSTR(xValue,1,nSize-1) + CHR(0)
- else
- xValue = REPLICATE(" ",nSize-1) + CHR(0)
- endif
- nBytes := SetStructString( this.value, ;
- nOffset, ;
- nLength, ;
- xValue, ;
- nSize )
- endcase
- endif
- return ( xValue )
-
-
- function findIndex( sName )
- local i, bFound
- bFound = false
- for i = 1 to this.member.size
- if ( this.member[i].name == sName )
- bFound = true
- exit
- endif
- next
- if ( NOT bFound )
- i := -1
- endif
- return ( i )
-
- function length
- local nLength
- nLength = 0
- for i = 1 to this.member.size
- nLength = nLength + this.member[i].size
- next
- return nLength
-
- endclass
-
- class StructMember( sName, nType, nSize )
- this.name = sName
- this.type = nType
- this.size = 0
- this.offset = 0
- do case
- case ( nType == TYPE_WORD )
- this.size := SIZEOF_WORD
- case ( nType == TYPE_DWORD )
- this.size := SIZEOF_DWORD
- case ( nType == TYPE_LONG )
- this.size := SIZEOF_LONG
- case ( nType == TYPE_STRING )
- this.size := nSize
- endcase
- endclass
-
-